﻿namespace ClassLibrary.DataLayer.DataObjects
{
    using ClassLibrary.BusinessLayer.BusinessObjects;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;

    public class ProcedureSetDAO : IProcedureSetDAO
    {
        public bool DeleteProcedureSet(ProcedureSet clsProcedureSet)
        {
            return clsProcedureSet.Save("D");
        }

        public int DeleteProcedureSet(string SPName, DbParameter[] param)
        {
            ProcedureSet clsProcedureSet = new ProcedureSet();
            return Db.ExecuteNonQuery(clsProcedureSet.UpdateQuery(SPName, param));
        }

        public IList<ProcedureSet> GetProcedureSet(ProcedureSet clsProcedureSet)
        {
            DataTable dt = Db.GetDataTable(clsProcedureSet.CreateGetCommandParameters());
            clsProcedureSet.LoadDataToCollection(dt.Rows[0]);
            if (dt.Rows[0] == null)
            {
                throw new Exception("ProcedureSet doesn't exist");
            }
            IList<ProcedureSet> list = new List<ProcedureSet>();
            list.Add(clsProcedureSet);
            return list;
        }

        public IList<ProcedureSet> GetProcedureSet(string headingSet)
        {
            ProcedureSet clsProcedureSet = new ProcedureSet();
            clsProcedureSet._HeadingSet = headingSet;
            return this.GetProcedureSet(clsProcedureSet);
        }

        public IList<ProcedureSet> GetsProcedureSet()
        {
            ProcedureSet clsProcedureSet = new ProcedureSet();
            IDataReader dtReader = Db.GetDataReader(clsProcedureSet.CreateGetsCommandParameters());
            IList<ProcedureSet> list = new List<ProcedureSet>();
            while (dtReader.Read())
            {
                clsProcedureSet = new ProcedureSet();
                clsProcedureSet.LoadDataToCollection(dtReader);
                list.Add(clsProcedureSet);
            }
            return list;
        }

        public DataTable GetsProcedureSet(string SPName, DbParameter[] param)
        {
            ProcedureSet clsProcedureSet = new ProcedureSet();
            return Db.GetDataTable(clsProcedureSet.GetsDataReader(SPName, param));
        }

        public bool InsertProcedureSet(ProcedureSet clsProcedureSet)
        {
            return clsProcedureSet.Save("I");
        }

        public int InsertProcedureSetReturnIdentity(ProcedureSet clsProcedureSet)
        {
            return clsProcedureSet.Save("I", true);
        }

        public string InsertProcedureSetReturnOutput(ProcedureSet clsProcedureSet)
        {
            return clsProcedureSet.Save("I", "@ProcedureSetID");
        }

        public IList<ProcedureSet> ListProcedureSet(ProcedureSet clsProcedureSet)
        {
            IDataReader dtReader = Db.GetDataReader(clsProcedureSet.CreateListCommandParameters());
            IList<ProcedureSet> list = new List<ProcedureSet>();
            while (dtReader.Read())
            {
                ProcedureSet clsProcedureSets = new ProcedureSet();
                clsProcedureSets.LoadDataToCollection(dtReader);
                list.Add(clsProcedureSets);
            }
            return list;
        }

        public IList<ProcedureSet> ListProcedureSet(string headingSet)
        {
            ProcedureSet clsProcedureSet = new ProcedureSet();
            clsProcedureSet._HeadingSet = headingSet;
            return this.ListProcedureSet(clsProcedureSet);
        }

        public bool UpdateProcedureSet(ProcedureSet clsProcedureSet)
        {
            return clsProcedureSet.Save("U");
        }

        public int UpdateProcedureSet(string SPName, DbParameter[] param)
        {
            ProcedureSet clsProcedureSet = new ProcedureSet();
            return Db.ExecuteNonQuery(clsProcedureSet.UpdateQuery(SPName, param));
        }
    }
}

